<template>
  <xd-layout theme="dark" :logo="logo" :sidebar-list="sidebarList" :logout="logout" />
</template>

<script>
import {mapGetters} from 'vuex'
import Layout from '@/layouts/Layout'
import { user } from '../../api/'

export default {
  name: 'Main',
  data () {
    return {
      isDev: process.env.NODE_ENV === 'development',
      logo: {
        src: null,
        alt: '',
        firstDes: '',
        secondDes: '用户管理系统'
      }
    }
  },
  computed: {
    ...mapGetters({
      userType: 'user/getUserType',
      getSidebarList: 'menu/getSidebarList'
    }),
    sidebarList () {
      return this.getSidebarList(this.userType).main
    }
  },
  mounted () {
    this.checkRouter()
  },
  created () {
    this.getSchoolNameAndLogo()
  },
  methods: {
    // 检查路由配置
    checkRouter () {
      if (
        window.location.hash === '#/main' ||
        window.location.hash === '#/main/'
      ) {
        try {
          this.$router.replace(this.sidebarList[0].path)
        } catch (error) {
          if (process.env.NODE_ENV !== 'production') {
            throw Error(
              `请检查菜单配置是否正确，配置路由: ${
                window.location.origin
              }/#/config`
            )
          }
        }
      }
    },
    // 获取标题logo
    getSchoolNameAndLogo () {
      user()._getSchoolLogo().then(res => {
        if (res.status) {
          this.logo = {
            src: `${this.isDev ? '/root' : ''}/${res.data.logoPath}`,
            alt: res.data.schoolName,
            firstDes: res.data.schoolName,
            secondDes: '用户管理系统'
          }
        }
      })
    },
    logout () {
      user()._logout().then((res) => {
        if (res.status) {
          this.$message.success('退出成功！')
          window.sessionStorage.clear()
          window.location.href = res.data // '/loginOut'
        }
      })
    }
  },
  components: {
    'xd-layout': Layout
  }
}
</script>
